//weex接口上传
let stream = weex.requireModule('stream'); 
//weex缓存
let storage = weex.requireModule('storage');
// weex弹窗
let modal = weex.requireModule("modal");

// 加密解密  Decrypt 解密 Encrypt加密
import { Decrypt, Encrypt  }  from "@/utils/index.js"
// 编译数据
import { serialize }  from "@/utils/index.js"

// console.log(Encrypt('52ACAF539A06FBFA60573E5A6BCE63D7F1FDEF561DA51B2BD8197C8DD13B0C8B123456'))

let setUrl="http://pregateway.huicai86.cn:61015/";

// 缓存token
export let getItem = (name) => {
    return new Promise((resolve, reject) => {
        storage.getItem(name, event => {
            if (event.result === "success") {
                resolve(event.data);
            } else {
                reject("获取数据失败");
                // modal.toast({ message: "请重新登录" });
            }
        })
    })
}

// get请求
export const GetResponsesToken = (url, params) => {
    let param = typeof params == 'object' ?  `?${serialize(params)}` : ''
    return new Promise((resolve, reject) => {
        getItem('token').then(vaule => {
            stream.fetch({
                method: 'GET',
                type: 'json',
                url: `${setUrl}${url}${param}`,
                body: '',
                headers: {
                    "Content-Type"  : "application/json;charset=utf-8",
                    "Authorization" : vaule
                }
            }, function (data) {
                resolve(data.data)
            })
        })
    })
}

export const GetResponsesNotToken = (url, params) => {
    let param = typeof params == 'object' ?  `?${serialize(params)}` : ''
    return new Promise((resolve, reject) => {
        stream.fetch({
            method: 'GET',
            type: 'json',
            url: `${setUrl}${url}${param}`,
            body: '',
            headers: {
                "Content-Type"  : "application/json;charset=utf-8"
            }
        }, function (data) {
            resolve(data.data)
        })
    })
}


// Post请求
export const postResponsesToken = (url, params) => {
    return new Promise((resolve, reject) => {
        getItem('token').then(vaule => {
            stream.fetch({
                method: 'POST',
                type: 'json',
                url: `${setUrl}${url}`,
                body: JSON.stringify( params || {} ) ,
                headers: {
                    "Content-Type"  : "application/json;charset=utf-8",
                    "Authorization" : vaule
                }
            }, function (data) {
                resolve(data.data)
            })
        })
    })
}

// 自定义头
export const customResponse = (obj) => {
    let { url, type, params, header } = obj

    return new Promise((resolve, reject) => {
        stream.fetch({
            method: type,
            type: 'json',
            url: `${setUrl}${url}`,
            body: params || '',
            headers: header
        }, function (data) {
            resolve(data.data)
        })
    })
}
